package com.zyht.bank.manage.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.zyht.bank.entity.AxdCustLoanExample;
import com.zyht.bank.entity.AxdNoticeTime;
import com.zyht.bank.service.AxdCustLoanService;
import com.zyht.bank.service.AxdNoticeTimeService;
import com.zyht.common.AbsManageBaseController;
import com.zyht.common.intercepter.Token;
import com.zyht.common.util.Page;

/**
 * 催交时间管理
 * 
 * @author qjl
 * @data 2017年7月11日
 */
@Controller
@RequestMapping("/admin/notice")
public class AdminAxdNoticeTimeController extends AbsManageBaseController {
	private String BASEPATH = "/banks/manage/noticetime/";
	@Resource
	private AxdNoticeTimeService axdNoticeTimeService;

	@Resource
	private AxdCustLoanService axdCustloanService;

	@Token(save = true)
	@RequestMapping(value = "/index", method = RequestMethod.GET)
	public String index(Model model, Integer currentPage, HttpSession session) {
		// 统计人数
		AxdCustLoanExample example = new AxdCustLoanExample();
		example.createCriteria().andIsCheckEqualTo("0").andIsLoanEqualTo("1");
		int count = axdCustloanService.count(example);

		if (null == currentPage) {
			currentPage = 1;
		}
		int pageSize = getPageSize();
		PageHelper.startPage(currentPage, pageSize);
		com.github.pagehelper.Page<AxdNoticeTime> pageInfo = (com.github.pagehelper.Page<AxdNoticeTime>) axdNoticeTimeService
				.list(null);
		int total = Long.valueOf(pageInfo.getTotal()).intValue();
		Page<AxdNoticeTime> page = new Page<AxdNoticeTime>(total, currentPage, pageSize);
		page.setRecordList(pageInfo);
		model.addAttribute("page", page);
		model.addAttribute("count", count);
		return makeView("noticeindex");

	}

	/**
	 * 保存
	 * 
	 * @param
	 * @return
	 */
	@Token(remove = true)
	@RequestMapping(value = "edit/1", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, String> edit(AxdNoticeTime axdNoticeTime, Model model, HttpSession session) {
		Map<String, String> map = new HashMap<String, String>();
		if (null != axdNoticeTime.getId()) {
			AxdNoticeTime noticeTime = axdNoticeTimeService.findOneById(axdNoticeTime.getId());
			noticeTime.setCreateTime(new Date());
			noticeTime.setStatus(axdNoticeTime.getStatus());
			axdNoticeTimeService.updateById(axdNoticeTime);
			map.put("status", "1");
			model.addAttribute("msg", "操作成功！");
			return map;
		}
		map.put("status", "0");

		if (null == axdNoticeTime.getNoticeTime()) {
			map.put("msg", "提醒时间不能为空");
			return map;
		}
		try {
			axdNoticeTime.setCreateTime(new Date());
			axdNoticeTime.setStatus("0");
			axdNoticeTimeService.addOne(axdNoticeTime);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("msg", "内部异常，操作失败！");
			return map;
		}

		map.put("status", "1");
		model.addAttribute("msg", "操作成功！");
		return map;

	}

	/**
	 * 删除
	 * 
	 * @param id
	 * @return
	 */
	@Token(remove = true)
	@RequestMapping(value = "edit/2", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, String> del(Integer id, Model model, HttpSession session) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("status", "0");

		if (id == null) {
			map.put("msg", "非法的操作！");
			return map;
		}
		AxdNoticeTime noticeTime = axdNoticeTimeService.findOneById(id);
		if (null == noticeTime) {
			map.put("msg", "非法的操作！");
			return map;
		}
		try {
			axdNoticeTimeService.deleteById(id);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("status", "0");
			map.put("msg", "内部异常，操作失败！");
			return map;
		}
		map.put("status", "1");
		return map;

	}

	private String makeView(String path) {
		return BASEPATH.concat(path);
	}
}
